package com.robot.botrunning.service.impl.botRunning;

import com.robot.botrunning.pojo.Robot;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.robot.botrunning.service.botRunning.botRunningService;
import com.robot.botrunning.utils.runningPool;
@Service
public class botRunningServiceImpl implements botRunningService{
    public final static runningPool runningPool = new runningPool();
    @Override
    public String addBot(int bid,int uid,String botCode, String map,int myX,int myY,int oppoX,int oppoY,String steps,String pos,String lang) {
        int[][] gameMap = new int[13][14];//字符串转二维数组
        map = map.replaceAll("[\\[,\\]]", "");//去除[与,
        map=map.replaceAll("\\s+", "");;//去除中间空格
        for (int i = 0; i < map.length(); i++) {
            // 获取当前字符并验证是否为数字
            char c = map.charAt(i);
            if (!Character.isDigit(c)) {
                continue;
            }
            // 转换字符为数字
            int value = Character.getNumericValue(c);

            // 计算行列位置
            int row = i / 14;    // 行索引（0~12）
            int col = i % 14;    // 列索引（0~13）
            // 填充数组
            gameMap[row][col] = value;
        }
        int[] step = steps.chars().map(Character::getNumericValue).toArray();
        Robot robot = new Robot();
        robot.setBotid(bid);
        robot.setUserid(uid);
        robot.setBotCode(botCode);
        robot.setMap(gameMap);
        robot.setMyX(myX);
        robot.setMyY(myY);
        robot.setOppoX(oppoX);
        robot.setOppoY(oppoY);
        robot.setSteps(step);
        robot.setPos(pos);
        robot.setLang(lang);
        runningPool.addRobot(robot);

        return "success";
    }
}
